<?php
class ProductAction extends GlobalAction{
    public function index(){
    	$this->Product_list();
    }
 	public function product_list(){
 		function sortname($id){
 			$sort = M('sort');
 			$sort = $sort->where("sort_id = '$id'")->getField('title');
 			return $sort;
 		}
    	$product = M('product');
    	import("@.ORG.Page"); //导入分页类 
        $count = $product->count();    //计算总数 
        $p = new Page ( $count, 20 ); 
    	$product = $product->limit($p->firstRow.','.$p->listRows)->order('sorting asc,product_id desc')->findall();
    	$page = $p->show (); 
        $this->assign( "page", $page ); 
    	$this->assign('product',$product);
    	$this->GlobalName();
		$this->display('list');
    }
 	public function product_search(){
 		function sortname($id){
 			$sort = M('sort');
 			$sort = $sort->where("sort_id = '$id'")->getField('title');
 			return $sort;
 		}
        $search['title'] = array('like','%'.$_POST['search'].'%');
        $search['e_title'] = array('like','%'.$_POST['search'].'%');
        $search['brand'] = array('like','%'.$_POST['search'].'%');
        $search['overview'] = array('like','%'.$_POST['search'].'%');
        $search['e_overview'] = array('like','%'.$_POST['search'].'%');
        $search['contents'] = array('like','%'.$_POST['search'].'%');
        $search['e_contents'] = array('like','%'.$_POST['search'].'%');
        $search['_logic'] = 'or';
    	$product = M('product');
    	import("@.ORG.Page"); //导入分页类 
        $count = $product->where($search)->count();    //计算总数 
        $p = new Page ( $count, 20 ); 
    	$product = $product->where($search)->limit($p->firstRow.','.$p->listRows)->order('sorting asc,product_id desc')->findall();
    	$page = $p->show (); 
        $this->assign( "page", $page ); 
    	$this->assign('product',$product);
    	$this->GlobalName();
		$this->display('list');
    }
    
    public function add(){
    	$sort = M('sort');
    	$sort1 = $sort->where("sid = ''")->order('sorting asc,sort_id asc')->findall();
    	$sort2 = $sort->where("sid != ''")->order('sorting asc,sort_id asc')->findall();
    	$this->assign('sort1',$sort1);
    	$this->assign('sort2',$sort2);
		$property = M('product_property');
		$property_list = $property->findall();
    	$this->assign('property_list',$property_list);
    	$this->GlobalName();
		$this->display();
    }
    public function product_news_get(){
    	$_POST['time'] = time();
    	$product = M('product');
    	$product->create();
    	if ($product->add()) {
    		echo mysql_insert_id();
    	};
    }
    
    public function edit(){
    	$pid = $_GET['pid'];
    	$product = M('product');
    	$product = $product->where("product_id = '$pid'")->findall();
    	$sort = M('sort');
    	$sort1 = $sort->where("sid = ''")->order('sorting asc,sort_id asc')->findall();
    	$sort2 = $sort->where("sid != ''")->order('sorting asc,sort_id asc')->findall();
    	$this->assign('sort1',$sort1);
    	$this->assign('sort2',$sort2);
    	$pic = M('product_picture');
    	$pic = $pic->where("pid = '$pid'")->findall();
        $this->assign("pic", $pic); 
    	$this->assign('product',$product);
    	$this->GlobalName();
		$this->display();
    }
    public function product_edit_get(){
    	$product = M('product');
    	$product->create();
    	if ($product->save()) {
    		$this->get_alert('修改成功');
    	};
    }
    
    public function product_delete(){
    	$product = M('product');
    	$del = $_POST['del'];
    	$delete = $del[0];
    	for ($i=1;$i < count($del);$i++){
    		$delete = $delete.','.$del[$i];
    	}
    	if ($product->delete($delete)) {
    		$this->get_alert('删除成功');
    	};
    }
    
    
    public function sort_list(){
    	$sort = M('sort');
    	$sort1 = $sort->where("sid = ''")->order('sorting asc,sort_id asc')->findall();
    	$sort2 = $sort->where("sid != ''")->order('sorting asc,sort_id asc')->findall();
    	$this->assign('sort1',$sort1);
    	$this->assign('sort2',$sort2);
    	$this->GlobalName();
		$this->display();
    }
    public function sort_add(){
    	$sort = M('sort');
    	$sort1 = $sort->where("sid = ''")->order('sorting asc,sort_id asc')->findall();
    	$sort2 = $sort->where("sid != ''")->order('sorting asc,sort_id asc')->findall();
    	$this->assign('sort1',$sort1);
    	$this->assign('sort2',$sort2);
    	$this->GlobalName();
		$this->display();
    }
    public function sort_news_get(){
    	$sort = M('sort');
    	$sort->create();
    	if ($sort->add()) {
    		$this->get_alert('新增成功');
    	};
    }
    public function sort_edit(){
    	$sortid = $_GET['sortid'];
    	$sort = M('sort');
    	$sort1 = $sort->where("sid = ''")->order('sorting asc,sort_id asc')->findall();
    	$sort2 = $sort->where("sid != ''")->order('sorting asc,sort_id asc')->findall();
    	$this->assign('sort1',$sort1);
    	$this->assign('sort2',$sort2);
    	$sort = $sort->where('sort_id = '.$sortid)->findall();
    	$this->assign('sort',$sort);
    	$this->GlobalName();
		$this->display();
    }
    public function sort_edit_get(){
    	$sort = M('sort');
    	$sort->create();
    	if ($sort->save()) {
    		$this->get_alert('修改成功');
    	};
    }
    public function sort_delete(){
    	$sort = M('sort');
    	$del = $_POST['del'];
    	$delete = $del[0];
    	for ($i=1;$i < count($del);$i++){
    		$delete = $delete.','.$del[$i];
    	}
    	if ($sort->delete($delete)) {
    		$this->get_alert('删除成功');
    	};
    }
    
    
    public function ajax_picture_up(){
    	if (!empty($_FILES['picture_get']['name'])) {
    		$_POST['photourl'] = $this->_upload('0');
    	}
    	$_POST['time'] = time();
    	$pic = M('product_picture');
		$new = $pic->where("pid = '".$_POST['pid']."' and defaultpic = '1'")->find();
		if(empty($new)){
			$_POST['defaultpic'] = '1';
		}
		if(isset($_POST['defaultpic'])){
			$pic->where("pid = '".$_POST['pid']."'")->setField('defaultpic','0');
		}
    	$pic->create();
    	if ($pic->add()){
    		echo json_encode(array('photourl'=>$_POST['photourl'],'pid'=>mysql_insert_id(),'defaultpic'=>$_POST['defaultpic']));
    	}
    }
	public function ajax_picture_del(){
		$pid = $_POST['pid'];
    	$pic = M('product_picture');
    	$link = M('system');
    	$link_m = $link->getField('images_savePath').'m_'.$_POST['photourl'];
    	$link_s = $link->getField('images_savePath').'s_'.$_POST['photourl'];
    	if ($pic->delete($pid)){
    		echo "删除成功";
    	};
    	unlink($link_m);
    	unlink($link_s);
    }
	public function ajax_picture_default(){
		$pic = M('product_picture');
		$pic->where("pid = '".$_POST['pid']."'")->setField('defaultpic','0');
    	$pic->where("photourl = '".$_POST['photourl']."'")->setField('defaultpic','1');
		
    }
    
    public function ajax_product_show(){
    	$this->ajax_get('product','show_m');
    }
    
    public function ajax_product_newsproduct_show(){
    	$this->ajax_get('product','newsproduct');
    }
    
    public function ajax_product_specialproduct_show(){
    	$this->ajax_get('product','specialproduct');
    }
   	//分类是否显示
    public function ajax_sort_show(){
    	$this->ajax_get('sort','show_m');
    }
	//添加新产品属性
	public function ajax_newproperty_get(){
    	$property = M('product_property');
		$property->create();
    	if ($property->add()) {
    		echo json_encode(array('property'=>$_POST['property'],'e_property'=>$_POST['e_property'],'property_id'=>mysql_insert_id(),'show_sort'=>$_POST['show_sort']));
    	};
    }
	//属性删除
	public function ajax_property_del(){
		$property = M('product_property');
		for($i=0;$i<count($_POST['property_id']);$i++){
			if($property->delete($_POST['property_id'][$i])){
			}
		}
		print_r($_POST['property_id']);
	}
	//产品属性添加显示
	public function ajxa_property_addshow(){
		
		$input = A('Input');
		$property = M('product_property');
		for($i=0;$i<count($_POST['property_id']);$i++){
			$property_name = $property->where("property_id = '".$_POST['property_id'][$i]."'")->Field('property,e_property,show_sort')->findall();
			$show = $show.'<tr>
              <td align="right" valign="middle">'.$property_name[0]['property'].'['.$property_name[0]['e_property'].']</td>
              <td align="left" valign="middle">'.$input->add_input($_POST['property_id'][$i],$property_name[0]['show_sort']).'</td>
             </tr>';
			 
		}
    	echo json_encode($show);
		//echo $show;
    }
	
    public function left(){//默认左列表
    	$left = array(
    		array('sort'=>'Product','name'=>'产品管理','value'=>'product_list'),
    		array('sort'=>'Product','name'=>'新增产品','value'=>'add'),
    		array('sort'=>'Product','name'=>'分类管理','value'=>'sort_list'),
    		array('sort'=>'Product','name'=>'新增分类','value'=>'sort_add')
    	);
     	$this->assign('user',$_SESSION['User']);
    	$this->assign('sort',$sort);
    	$this->assign('left',$left);
    	$this->display('Index:left');
    }
}
?>